<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    // 1.找到所有目标元素出现的下标值
    function getIndex(str, item) {
        let a = [];
        let index = str.indexOf(item, 0);
        a.push(index);

        while (index != -1) {
            index = str.indexOf(item, index + 1);
            if (index != -1) {
                a.push(index);
            }

        }
        console.log(a);
    }
    let str = 'hello world ! hello world   ! hello world!'
    getIndex(str, 'o');

    //找出字符串中每个字符以及出现的次数
    let obj = {};

    // function getStrCount(str) {
    //     str = str.toLocaleLowerCase();
    //     for (let i = 0; i < str.length; i++) {
    //         let key = str[i];
    //         if (obj[key]) {
    //             //对象中有这个字母
    //             obj[key]++;
    //         } else {
    //             //对象中没有这个字母,把字母加到对象中
    //             obj[key] = 1;
    //         }

    //     }
    //     for (let key in obj) {
    //         console.log(key + '----' + obj[key] + '次');
    //     }

    // }
    str = 'sdf ghb s dak  jf gds hf';
    getStrCount(str);

    function getStrCount(str) {
        str = str.toLocaleLowerCase();
        let arrStr = str.split('').sort();
        let count = 0;
        for (let i = 0; i < arrStr.length; i++) {
            if (arrStr[i] == arrStr[i + 1]) {
                count++;
            } else {
                console.log(arrStr[i] + ' -- ' + count);
                count = 1;
            }
        }
    }
</script>

</html>